package org.budo.support.web.realserverport;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
@Getter
@Setter
public class RealServerPortFilter implements Filter {
    private String[] realServerPortHeaderNames = new String[] { "x-server-port" };

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("#28 init, filterConfig=" + filterConfig + ", this=" + this);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        chain.doFilter(new RealServerPortRequestWrapper((HttpServletRequest) request, this.getRealServerPortHeaderNames()), response);
    }

    @Override
    public void destroy() {
        log.info("#38 destroy, this=" + this);
    }
}